﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTO;
using BUS;
using System.Drawing;

namespace TuyenDungViecLam.Admin
{
    public partial class EditUser : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                int userID = int.Parse(Request.QueryString["uid"].ToString());

                // dua du lieu len giao dien
                User_DTO user = User_BUS.GetUserInfomationByUserID(userID);
                txtUsername.Text = user.UserName;
                txtEmail.Text = user.Email;

                if (user.Status == 1)
                    CbActiveUser.Checked = true;
                else
                    CbActiveUser.Checked = false;
            }
        }

        protected void BtEditUser_Click(object sender, EventArgs e)
        {
            try
            {
                int userID = int.Parse(Request.QueryString["uid"].ToString());

                User_DTO newUser = new User_DTO();
                newUser.UserID = userID;
                newUser.UserName = txtUsername.Text;
                newUser.PassWord = txtPassword.Text;
                newUser.Date = DateTime.Now.Date;
                newUser.Email = txtEmail.Text;

                if (CbActiveUser.Checked)
                    newUser.Status = 1;
                else
                    newUser.Status = 0;

                bool kq = User_BUS.UpdateUser(newUser);

                if (kq == true)
                {
                    divCapnhat.Visible = false;
                    divThongbao.Visible = true;
                    lbThongbao.Text = "Cập nhật User thành công";
                    lbThongbao.ForeColor = Color.Blue;
                }
                else
                {
                    divThongbao.Visible = true;
                    lbThongbao.Text = "Cập nhật User thất bại";
                    lbThongbao.ForeColor = Color.Red;
                }
            }
            catch (Exception)
            {
                divThongbao.Visible = true;
                lbThongbao.Text = "Cập nhật User thất bại";
                lbThongbao.ForeColor = Color.Red;
            }
        }
    }
}
